#include <iostream>
#include <thread>
#include <mutex>

//饿汉式单例:线程安全
class singleton
{
public:
    static singleton* GetInstance()//static 不能使用普通变量
    {
        return p;
    }

    void Release()
    {
        delete p;
    }
private:
    singleton(){}
    static singleton *p;
};

singleton* singleton::p = new singleton;

void CreateObj()
{
    auto p1 = singleton::GetInstance();
    printf("address:%p\n",p1);

}
int main()
{
    #if 0
    auto p1 = singleton::GetInstance();
    auto p2 = singleton::GetInstance();
    auto p3 = singleton::GetInstance();
    //p1:a42b3eb0 p2:a42b3eb0 p3:a42b3eb0
    #endif

    std::thread t[10];
    for(auto & thrd: t)
    {
        thrd = std::thread(CreateObj);
    }

    for(auto & thrd: t)
    {
        thrd.join();
    }
    
    return 0;
}